﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Company.StudyClub.Model;

namespace Company.StudyClub.DAL.Site
{
    public class ConfigDAL
    {
        public static ArrayList SearchConfigValues(int variableID)
        {
            ArrayList valueList = new ArrayList();

            SqlParameter[] parameters = {
					new SqlParameter("@VariableID", SqlDbType.Int,4)};
            parameters[0].Value = variableID;

            Company.StudyClub.Model.Site_Area model = new Company.StudyClub.Model.Site_Area();
            DataSet ds = DbHelperSQL.RunProcedure("sp_Site_ConfigValue_GetValueByVariableID", parameters, "ds");

            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                Site_ConfigValue valueEntity = new Site_ConfigValue();
                valueEntity.ValueID = Int32.Parse(dr["ValueID"].ToString());
                valueEntity.VariableID = Int32.Parse(dr["VariableID"].ToString());
                valueEntity.Level = Int32.Parse(dr["Level"].ToString());
                valueEntity.Area = dr["Area"].ToString();
                valueEntity.BeginDate = DateTime.Parse(dr["BeginDate"].ToString());
                valueEntity.EndDate = DateTime.Parse(dr["EndDate"].ToString());
                valueEntity.ServerName = dr["ServerName"].ToString();
                valueEntity.KeyValue = dr["KeyValue"].ToString();
                valueEntity.MemberType = dr["MemberType"].ToString();
                valueEntity.Language = dr["Language"].ToString();
                valueList.Add(valueEntity);
            }

            return valueList;           
        }

        public static Site_ConfigVariable GetModelByName(string keyName)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@VariableName", SqlDbType.NVarChar,20)};
            parameters[0].Value = keyName;

            Company.StudyClub.Model.Site_ConfigVariable model = new Company.StudyClub.Model.Site_ConfigVariable();
            using (SqlDataReader reader = DbHelperSQL.RunProcedure("sp_Site_ConfigVariable_GetModelByName", parameters))
            {
                if (reader.Read())
                {
                    if (reader["VariableID"].ToString() != "")
                    {
                        model.VariableID = int.Parse(reader["VariableID"].ToString());
                    }
                    model.VariableName = reader["VariableName"].ToString();
                    model.DefaultValue = reader["DefaultValue"].ToString();
                    model.AppName = reader["AppName"].ToString();

                    return model;
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
